<?php
namespace Home\Common;

class SaveRate extends Mysql{
	private $saveRateByChannel;
	private $saveRateByServer;
	
	public function __construct(){
		$this->saveRateByChannel=array();
		$this->saveRateByServer=array();
		parent::__construct();
	}
	//获取留存
	public function getSaveRate(){
		$dayLists	=	array(7,6,5,4,3,2,1,0);
		foreach($dayLists as $dayNum){
		//30天前留存统计
		//创建总人数统计
			$tarDay	=	date("Y-m-d",strtotime(session('mog_thedate')."-".$dayNum." days"));
			$result	=	$this->mysql->prepare("select count(distinct(userid)) count from mog_user_info where createtime = \"$tarDay\"");
			$result->execute();
		
			$row	=	$result->fetch(\PDO::FETCH_ASSOC);
			$sumUser	=	$row['count'];
		
		//1,3,7,14,30日留存人数统计;
			$saveDays	=	array(1,3,7,14,30);
			foreach($saveDays as $nums){
				$saveDay	=	date("Y-m-d",strtotime($tarDay."+".$nums." days"));
				$result		=	$this->mysql->prepare("select count(distinct(b.userid)) count 
						from mog_login a,mog_user_info b 
						where a.userid = b.userid and a.date = \"$saveDay\" and b.createtime = \"$tarDay\"");
				$result->execute();
			
				$row	=	$result->fetch(\PDO::FETCH_ASSOC);
				$saveUser[$nums]	=	$row['count'];
			}
			
			$saveRate[$dayNum]	=	array(
				1=>round($saveUser[1]/$sumUser*100,1),
				3=>round($saveUser[3]/$sumUser*100,1),
				7=>round($saveUser[7]/$sumUser*100,1),
				14=>round($saveUser[14]/$sumUser*100,1),
				30=>round($saveUser[30]/$sumUser*100,1),
				'sum'=>$sumUser,
				'date'	=>	$tarDay
			);
		}
		return $saveRate;
	}
}
?>